const util = require("../../../utils/util");

// order//order/deliverySet/index.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    money: '',//配送费
    free: '',//满多少免费
    packagMoney: '',//包装费
    miniMoney: '',//最低起送
    riderRange: 0,//骑手范围：0全部1审核通过骑手
    money3: '',
    money10: '',
    money50: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getDeliverySet();
  },

  bindMoney: function (e) {
    this.setData({
      money: e.detail.value
    })
  },
  bindMoney3: function (e) {
    this.setData({
      money3: e.detail.value
    })
  },
  bindMoney10: function (e) {
    this.setData({
      money10: e.detail.value
    })
  },
  bindMoney50: function (e) {
    this.setData({
      money50: e.detail.value
    })
  },
  bindFree: function (e) {
    this.setData({
      free: e.detail.value
    })
  },
  bindPackagMoney: function (e) {
    this.setData({
      packagMoney: e.detail.value
    })
  },
  bindMiniMoney: function (e) {
    this.setData({
      miniMoney: e.detail.value
    })
  },

  save: function () {
    let that = this;
    let money = that.data.money;
    let packMoney = that.data.packagMoney;
    let free = that.data.free;
    let mini = that.data.miniMoney;
    let firstMoney = that.data.money3;
    let secondMoney = that.data.money10;
    let thirdMoney = that.data.money50;
    if (money == '') {
      money = 0;
    }
    if (firstMoney == '') {
      firstMoney = 0;
    }
    if (secondMoney == '') {
      secondMoney = 0;
    }
    if (thirdMoney == '') {
      thirdMoney = 0;
    }
    if (packMoney == '') {
      packMoney = 0;
    }
    if (free == '') {
      free = 0;
    }
    if (mini == '') {
      mini = 0;
    }
    wx.request({
      url: app.globalData.host + '/api/ship/setShip',
      data: {
        uid: app.globalData.userInfo.Uid,
        shipMoney: money,
        packMoney: packMoney,
        freeLimit: free,
        minLimit: mini,
        riderRange: that.data.riderRange,
        firstMoney: firstMoney,
        secondMoney: secondMoney,
        thirdMoney: thirdMoney,
      },
      method: 'POST',
      success: function (res) {
        if (res.data.Code == 0) {
          wx.showToast({
            title: '保存成功',
            duration: 1000
          })
          // wx.reLaunch({
          //   url: '/pages/index/index',
          // })
        } else {
          util.showModal(res.data.Message);          
        }
      }
    })
  },

  getDeliverySet: function () {
    let that = this;
    wx.request({
      url: app.globalData.host + '/api/Ship/GetShip?uid=' + app.globalData.userInfo.Uid,
      success: function (res) {
        if (res.data.Code == 0) {
          if (res.data.Data) {
            let item = res.data.Data;
            that.data.riderRange = item.RiderRange;
            that.setData({
              money: item.ShipMoney,//配送费
              free: item.FreeLimit,//满多少免费
              packagMoney: item.PackMoney,//包装费
              miniMoney: item.MinLimit,//最低起送
              money3: item.FirstMoney,
              money10: item.SecondMoney,
              money50: item.ThirdMoney 
            })
          }
        } else {
          wx.showModal({
            title: '提示',
            content: res.data.Message,
            showCancel: false
          })
        }
      }
    })
  }
})